<template>
  <udialog :dialog.sync="dialog" width="600px" @handleBtnSure="btnSure" @handleBtnClose="clearAll">
    <el-form v-model="dialog.form" class="tableDialog" :disabled="disabled">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="form.author" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialog.visible = false">取 消</el-button>
      <el-button type="primary" size="small" @click="dialogSure(dialog.title)">确 定</el-button>
    </div>
  </udialog>
</template>

<script>
import Udialog from '@/components/Udialog'
export default {
  components: { Udialog },
  props: {
    dialog: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {
        title: '',
        author: ''
      },
      disabled: false
    }
  },
  watch: {
    dialog: {
      deep: true,
      handler(val, old) {
        if (val.visible && val.data) {
          this.form = this.$utils.deepClone(val.data)
          if (val.title === '查看') {
            this.disabled = !this.disabled
          }
        }
      }
    }
  },
  methods: {
    // 提交表单
    btnSure() {
      this.$confirm('确认保存').then(_ => {
        // ....
        // ... this.$emit('update')
        this.dialog.visible = false
      }).catch(_ => {})
    },
    // 清空数据
    clearAll() {
      // ...
      this.form.title = ''
      this.form.author = ''
      this.disabled = false
    }
  }
}
</script>
<style lang="scss">
.tableDialog {
  padding: 0 30px 0 0;
  .el-form-item { margin-bottom: 20px;}
  .el-form-item__label { width: 90px;  padding-right:20px;}
  .el-form-item__content { padding-left:90px;}
  .el-date-editor.el-input, .el-date-editor.el-input__inner { width: 100%;}
  .el-select { display: block;}
}
</style>
